package com.sanjishu.enterlab.electricity.sale.controller.sale;

import com.sanjishu.enterlab.electricity.sale.domain.*;
import com.sanjishu.enterlab.electricity.sale.service.*;
import com.sanjishu.enterlab.electricity.sale.shiro.ShiroAdmin;
import com.sanjishu.enterlab.electricity.sale.utils.Page;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping("sale/companyInfo")
public class CompanyRelevanceController {

    @Autowired
    private CompanyBakService companyBakService;
    @Autowired
    private SaleApplyService saleApplyService;
    @Autowired
    private ContractInfoService contractInfoService;
    @Autowired
    private ContractSettingService contractSettingService;
    @Autowired
    private CompanyInfoService companyInfoService;
    @Autowired
    private CityService cityService;
    @Autowired
    private UserSettingService userSettingService;

    @RequestMapping(value = "relevance", method = RequestMethod.GET)
    public String index(Model model) {
        Subject subject = SecurityUtils.getSubject();
        ShiroAdmin loginUser = (ShiroAdmin) subject.getPrincipal();
        UserSetting userSetting = userSettingService.findByUserId(loginUser.getId());
        Integer pageSize = (userSetting!= null && userSetting.getNum1() != null) ? userSetting.getNum1() : 0;
        Page<SaleApply> saleApplyPage = saleApplyService.fineSaleApplyByPage(loginUser.getId(),
                null, null, 1, pageSize);

        //Page<CompanyBak> companyBakPage = companyBakService.findCompanyBakByPage(loginUser.getId(), loginUser.getTypes(), null, 1);
        model.addAttribute("pageData", saleApplyPage);
        model.addAttribute("userSetting", userSetting);
        return "sale/comRelevance/index";
    }

    @RequestMapping(value = "checkRelevance", method = RequestMethod.POST)
    public String page(String searchText, Integer searchTypes, Integer page, Model model) {
        Subject subject = SecurityUtils.getSubject();
        ShiroAdmin loginUser = (ShiroAdmin) subject.getPrincipal();
        UserSetting userSetting = userSettingService.findByUserId(loginUser.getId());
        Integer pageSize = (userSetting!= null && userSetting.getNum1() != null) ? userSetting.getNum1() : 0;
        Page<SaleApply> saleApplyPage = saleApplyService.fineSaleApplyByPage(loginUser.getId(), searchText, searchTypes, page, pageSize);

        model.addAttribute("pageData", saleApplyPage);
        model.addAttribute("searchText", searchText);
        model.addAttribute("searchTypes", searchTypes);
        return "sale/comRelevance/checkRelevance";
    }

    @RequestMapping(value = "show/{companyId}", method = RequestMethod.GET)
    public String editContract(@PathVariable Integer companyId, Model model) {

        CompanyBak companyBak = companyBakService.getById(companyId);
        model.addAttribute("companyBak", companyBak);

        CompanyInfo companyInfo = companyInfoService.findByCompanyBakId(companyId);
        //DetailInfo detailInfo = companyInfoService.findByCompanyId(companyId);

        List<City> cityList = cityService.getCityList();
        model.addAttribute("cityList", cityList);

        if (companyBak != null) {
            City city = cityService.getAreaAndCity(companyBak.getCityId());
            model.addAttribute("city", city);
            if (city != null) {
                List<City> areaList = null;
                if (city.getParentId() == 1) {
                    areaList = cityService.getArea(city.getId());
                } else {
                    areaList = cityService.getArea(city.getParentId());
                }
                model.addAttribute("areaList", areaList);
            }
        }
        if (companyInfo != null) {
            DetailInfo detailInfo = companyInfoService.findByCompanyId(companyInfo.getId());
            City city = cityService.getAreaAndCity(companyInfo.getCityId());
            model.addAttribute("city", city);
            if (city != null) {
                List<City> areaList = null;
                if (city.getParentId() == 1) {
                    areaList = cityService.getArea(city.getId());
                } else {
                    areaList = cityService.getArea(city.getParentId());
                }
                model.addAttribute("areaList", areaList);
            }
            ContractInfo contractInfo = contractInfoService.findByCompanyAndTime(companyInfo.getId());
            if (contractInfo != null) {
                ContractSetting contractSetting = contractSettingService.findByContractId(contractInfo.getId());
                model.addAttribute("contractSetting", contractSetting);
            }
            model.addAttribute("detailInfo", detailInfo);
            model.addAttribute("contractInfo", contractInfo);
        }

        //List<City> cityList = cityService.getCityList();
        //model.addAttribute("cityList", cityList);
        model.addAttribute("companyInfo", companyInfo);
        //model.addAttribute("detailInfo", detailInfo);

        return "sale/comRelevance/showContract";
    }
}
